import dayjs from "dayjs";

export const weekNameList = [" ", "一", "二", "三", "四", "五", "六", "日"];

//初始化时间刻度
export const secondsList = [
  "00:00 - 01:00",
  "01:00 - 02:00",
  "02:00 - 03:00",
  "03:00 - 04:00",
  "04:00 - 05:00",
  "05:00 - 06:00",
  "06:00 - 07:00",
  "07:00 - 08:00",
  "08:00 - 09:00",
  "09:00 - 10:00",
  "10:00 - 11:00",
  "11:00 - 12:00",
  "12:00 - 13:00",
  "13:00 - 14:00",
  "14:00 - 15:00",
  "15:00 - 16:00",
  "16:00 - 17:00",
  "17:00 - 18:00",
  "18:00 - 19:00",
  "19:00 - 20:00",
  "20:00 - 21:00",
  "21:00 - 22:00",
  "22:00 - 23:00",
  "23:00 - 24:00",
];

//计算选中时间的样式
export const getStyle = (e, c) => {
  let left = e.week * 12.5 + "%";
  let d = new Date(c.startTimeSlot);
  let top = d.getHours() * 120 + d.getMinutes() * 2;
  +"px";
  let height =
    dayjs(c.endTimeSlot).diff(dayjs(c.startTimeSlot), "minute") * 2 - 4 + "px";
  let objStyle = {
    left,
    top,
    height,
    background: hexToRgba(c.colour, 0.25),
    borderLeft: `3px solid ${c.colour}`,
  };
  return { objStyle, height };
};

//会员卡可看表
export const lookCard = [
  { id: "1", name: "本周课表" },
  { id: "2", name: "全部课表" },
];

//发布课表时间
export const publishSchedule = [
  { id: 1, name: "周一" },
  { id: 2, name: "周二" },
  { id: 3, name: "周三" },
  { id: 4, name: "周四" },
  { id: 5, name: "周五" },
  { id: 6, name: "周六" },
  { id: 7, name: "周日" },
];

//单节课时长
export const courseDuration = [
  { label: "15", value: 15 },
  { label: "30", value: 30 },
  { label: "45", value: 45 },
  { label: "60", value: 60 },
  { label: "75", value: 75 },
  { label: "90", value: 90 },
  { label: "105", value: 105 },
  { label: "120", value: 120 },
  { label: "135", value: 135 },
  { label: "150", value: 150 },
  { label: "165", value: 165 },
  { label: "180", value: 180 },
  { label: "195", value: 195 },
  { label: "210", value: 210 },
  { label: "225", value: 225 },
  { label: "240", value: 240 },
  { label: "255", value: 255 },
  { label: "270", value: 270 },
  { label: "285", value: 285 },
  { label: "300", value: 300 },
];
//禁用时间区间
export const disableWorkTime = (workStart, workEnd, interval) => {
  const workStartHour = workStart.hour();
  const workStartMinute = workStart.minute();
  const workEndHour = workEnd.hour();
  const workEndMinute = workEnd.minute();

  const disabledHours = () => {
    const hours = [];
    for (let i = 0; i < 24; i++) {
      if (i < workStartHour || i > workEndHour) {
        hours.push(i);
      }
    }
    return hours;
  };

  const disabledMinutes = (selectedHour) => {
    const minutes = [];
    for (let i = 0; i < 60; i++) {
      if (i % interval !== 0) {
        minutes.push(i);
      }
    }
    if (selectedHour === workStartHour) {
      for (let i = 0; i < workStartMinute; i += interval) {
        minutes.push(i);
      }
    }
    if (selectedHour === workEndHour) {
      for (let i = workEndMinute + interval; i < 60; i += interval) {
        minutes.push(i);
      }
    }
    return minutes;
  };

  return { disabledHours, disabledMinutes };
};

//计算一周时间周期
export const getWeekLabel = (date) => {
  let time = dayjs(date).format("MM-DD");
  let day = dayjs(date).day() == 0 ? 7 : dayjs(date).day();
  let weekName = weekNameList[day];
  return time + " " + "(" + weekName + ")";
};

//颜色工具转换
export const hexToRgba = (hex, opacity) => {
  return (
    "rgba(" +
    parseInt("0x" + hex.slice(1, 3)) +
    "," +
    parseInt("0x" + hex.slice(3, 5)) +
    "," +
    parseInt("0x" + hex.slice(5, 7)) +
    "," +
    opacity +
    ")"
  );
};

//计算总课表区域样式
export const getRoomStyle = (c) => {
  let d = new Date(c.startTimeSlot);
  let top = d.getHours() * 120 + d.getMinutes() * 2 + 60 + "px";
  let height =
    dayjs(c.endTimeSlot).diff(dayjs(c.startTimeSlot), "minute") * 2 - 4 + "px";
  let objStyle = {
    left: 0,
    top,
    height,
    background: hexToRgba(c.colour, 0.25),
    borderLeft: `3px solid ${c.colour}`,
  };
  return { objStyle, height };
};

//寻找相反的颜色
export const getComplementaryColor = (hex) => {
  // 确保输入是有效的十六进制颜色代码
  if (!/^#?[0-9A-Fa-f]{6}$/.test(hex)) {
    return null; // 或者抛出一个错误，或者返回一个默认值
  }

  // 去除可能的'#'前缀
  hex = hex.replace("#", "");

  // 将十六进制字符串拆分为RGB分量
  let r = parseInt(hex.slice(0, 2), 16);
  let g = parseInt(hex.slice(2, 4), 16);
  let b = parseInt(hex.slice(4, 6), 16);

  // 计算每个分量的补码
  let complementR = 255 - r;
  let complementG = 255 - g;
  let complementB = 255 - b;

  // 将补码转换回十六进制，并确保它们是两位数
  let complementRHex = complementR.toString(16).padStart(2, "0");
  let complementGHex = complementG.toString(16).padStart(2, "0");
  let complementBHex = complementB.toString(16).padStart(2, "0");

  // 组合成完整的十六进制颜色代码，并添加'#'前缀
  return `#${complementRHex}${complementGHex}${complementBHex}`;
};

//合并排课表和占用时间
export function mergeArrays(arr1, arr2, type) {
  // 如果任意一个数组为空，直接返回另一个数组的拷贝
  if (arr1.length === 0) return [...arr2.map((item) => ({ ...item }))];
  if (arr2.length === 0) return [...arr1];

  const arr3 = [...arr1];

  arr2.forEach((item2) => {
    const index = arr3.findIndex((item1) => item1[type] === item2[type]);

    if (index !== -1) {
      arr3[index].list = [...arr3[index].list, ...item2.list];
    } else {
      arr3.push({
        [type]: item2[type],
        list: item2.list,
        week: item2.week, // 添加一个默认的 week 值，如果 arr2 没有 week 信息
      });
    }
  });

  return arr3;
}
export const courseTypeList = { 1: "私", 2: "团", 3: "班", 4: "占" };
